package com.objectproject.main.service;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.objectproject.main.dao.MyObjectHierarchyDAOImpl;
import com.objectproject.main.exception.ObjectHierarchyException;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.service.iface.MyObjectHierarchyService;
import com.objectproject.main.service.subsystem.policy.PolicyAndConfigurationRepository;
import com.objectproject.main.service.subsystem.validation.ObjectHierarchyValidator;

@Service
public class MyObjectHierarchyServiceImpl implements MyObjectHierarchyService{

	@Autowired
	MyObjectHierarchyDAOImpl myObjectHierarchyDAOImpl;
	
	@Autowired
	ObjectHierarchyValidator objectHierarchyValidator;
	
	@Override
	public void isA(MyObject myObject, MyObject superObject) {
		
		//검증단계 생략 추후 구현
	
		myObjectHierarchyDAOImpl.insertHierarchy(myObject.getObjectId(), superObject.getObjectId());
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void isA(MyObject myObject, ArrayList<MyObject> superObjects) throws ObjectHierarchyException {
		
		for(MyObject superObject:superObjects){
			objectHierarchyValidator.beforeInsert(myObject, superObject);
		}
		
		//검증단계 생략 추후 구현
		for(MyObject superObject:superObjects){
			myObjectHierarchyDAOImpl.insertHierarchy(myObject.getObjectId(), superObject.getObjectId());
		}
	}

	@Override
	public void isNotAnymore(MyObject myObject, MyObject superObject) {
		
		//검증단계 생략 추후 구현
		
		myObjectHierarchyDAOImpl.deleteHierarchy(myObject.getObjectId(), superObject.getObjectId());
	}
	

	@Override
	public void isNotAnymore(MyObject myObject, ArrayList<MyObject> superObjects) {
		
		//검증단계 생략 추후 구현
		for(MyObject superObject:superObjects){
			myObjectHierarchyDAOImpl.deleteHierarchy(myObject.getObjectId(), superObject.getObjectId());
		}
	}
	
}
